package com.xiaoxing.welcome.mvp.ui;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.widget.TextView;

import com.xiaoxing.welcome.R;

import me.jessyan.armscomponent.commonres.view.popup.BasePopupWindow;
import me.jessyan.armscomponent.commonres.view.popup.OverShootInterpolator;


public class MainPopup extends BasePopupWindow implements View.OnClickListener {
    private View popupView;
    private Activity mActivity;
    private TextView tv_nei_rong;
    private MessageOk mMessageOk;
    private String mNeiRong;

    public interface MessageOk {
        void messageOk();
    }


    public MainPopup(Activity context, String neiRong, MessageOk messageOk) {
        super(context);
        this.mActivity = context;
        this.mMessageOk = messageOk;
        this.mNeiRong = neiRong;

        setPopupWindowFullScreen(true);
        bindEvent();
    }

    @Override
    protected Animation initShowAnimation() {
        return null;
    }

    @Override
    public Animator initShowAnimator() {
        AnimatorSet set;
        set = new AnimatorSet();
        ObjectAnimator transAnimator = ObjectAnimator.ofFloat(mAnimaView, "translationY", 250, 0).setDuration(600);
        transAnimator.setInterpolator(new OverShootInterpolator());
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(mAnimaView, "alpha", 0.4f, 1).setDuration(250 * 3 / 2);
        set.playTogether(transAnimator, alphaAnimator);
        return set;
    }

    @Override
    public Animator initExitAnimator() {
        AnimatorSet set;
        set = new AnimatorSet();
        ObjectAnimator transAnimator = ObjectAnimator.ofFloat(mAnimaView, "translationY", 0, 250).setDuration(600);
        transAnimator.setInterpolator(new OverShootInterpolator(-6));
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(mAnimaView, "alpha", 1f, 0).setDuration(800);
        set.playTogether(transAnimator, alphaAnimator);
        return set;
    }

    @Override
    public View getClickToDismissView() {
        return getPopupWindowView();
    }

    @Override
    public View onCreatePopupView() {
        popupView = createPopupById(R.layout.popup_main);
        return popupView;
    }

    @Override
    public View initAnimaView() {
        return findViewById(R.id.popup_anima);
    }

    private void bindEvent() {
        if (popupView != null) {

            popupView.findViewById(R.id.btn_que_ding).setOnClickListener(this);
            tv_nei_rong = (TextView) popupView.findViewById(R.id.tv_nei_rong);

            if (!TextUtils.isEmpty(mNeiRong))
                tv_nei_rong.setText(mNeiRong);

        }
    }


    @Override
    public void onClick(View v) {
        int i = v.getId();

        if (i == R.id.btn_que_ding) {
            mMessageOk.messageOk();
            dismiss();
        }

    }
}
